function register() {
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;
    var avatar = document.getElementById('avatar').value;
    var email = document.getElementById('email').value;
    var attribute = document.getElementById('attribute').value;
    var sex = document.getElementById('sex').value;
    var phone = document.getElementById('phone').value;
    var realName = document.getElementById('realName').value;

    $.ajax({
      type: "post",
      url: "http://localhost:8080/register", // 注意这里的URL地址，应该使用完整的URL，包括协议部分如http://
      data: JSON.stringify({
        username: username,
        password: password,
        avatar: avatar,
        email: email,
        attribute: attribute,
        sex: sex,
        phone: phone,
        realName: realName
    }),
    contentType: "application/json",
      success: function(data) {
        console.log(data);
        if(data.code == 0){
            window.location.href = '登录1.html'; // 登录成功后跳转到首页.html
        }else{
            // 处理登录失败的逻辑，例如显示错误信息
        console.error('登录失败:', data.message);
        alert('登录失败: ' + data.message);
        }
        
        // 处理登录成功后的逻辑，例如跳转页面或显示提示信息
      },
      error: function(xhr, status, error) {
        console.error('登录请求发生错误:', error);
        // 处理登录失败的逻辑，例如显示错误信息
        alert('登录请求发生错误，请稍后重试');
      }
    });
  }

  function uploadFile() {
    const fileInput = document.getElementById('avatar_url');
    const file = fileInput.files[0];
    const formData = new FormData();
    formData.append('file', file);

    fetch('http://localhost:8080/upload', {
        method: 'POST',
        body: formData
    })
    .then(response => {
        if(response.ok) {
            return response.json(); // 将响应转换为 JSON 格式
        } else {
            throw new Error('文件上传失败');
        }
    })
    .then(data => {
       
        document.getElementById('avatar').value = data.data;
        document.getElementById('avatar_url_image').src = "http://localhost:8080/download/" +data.data ;
        

        // 文件上传成功
        console.log('文件上传成功');
    })
    .catch(error => {
        console.error('上传出错:', error);
    });
}